package nioprocess;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
public class NioProcess {
    public static void transGBK2utf8(FileChannel from,
                                               FileChannel to)
            throws IOException 
{
 MappedByteBuffer buffer= from.map(FileChannel.MapMode.READ_ONLY, 0, from.size());
 Charset chset1=Charset.forName("GBK");
 CharsetDecoder dec=chset1.newDecoder();
 CharBuffer sbuf=dec.decode(buffer);
 Charset chset2=Charset.forName("utf8");
 CharsetEncoder enc=chset2.newEncoder();
 ByteBuffer tbuf=enc.encode(sbuf);
 to.write(tbuf);
}
    public static void insertTarget(FileChannel from,
                                      FileChannel to,
                                      String start,
                                      String ends)
            throws IOException 
{
 byte[] bs=start.getBytes();
 byte[] be=ends.getBytes();
 ByteBuffer buf=ByteBuffer.allocate(bs.length+be.length+(int)from.size());
 buf.put(bs);
 from.read(buf);
 buf.put(be);
 buf.flip();
 to.write(buf);
}
}
